package com.by.yz.activity.regitser;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.by.yz.R;
import com.by.yz.activity.LoginActivity;
import com.by.yz.activity.base.BaseActivity;
import com.by.yz.common.MFGT;
import com.by.yz.loginUtil.AccountRESTFul;
import com.by.yz.loginUtil.codeRESTFul;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by apple on 2016/10/4.
 */

public class RegisterActivity extends BaseActivity {
    private EditText register_phonenum;
    private EditText register_vercode;
    private Button getCode;
    private Button register_next1;
    private TextView register_account_have;
    private String CODE = "";
    private String phoneNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        register_phonenum = (EditText) findViewById(R.id.register_phonenum);//手机号码
        register_vercode = (EditText) findViewById(R.id.register_vercode);//填写验证码
        getCode = (Button) findViewById(R.id.getCode);//获取验证码
        register_next1 = (Button) findViewById(R.id.register_next1);//下一步
        register_account_have = (TextView) findViewById(R.id.register_account_have);//忘记密码

        //计时初始化
        final timeCount timecount = new timeCount(60000, 1000);
        getCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                phoneNumber=register_phonenum.getText().toString();
                Log.e("ggg",String.valueOf(phoneNumber.length()));
                if(phoneNumber.length()!=11){
                    new MaterialDialog.Builder(RegisterActivity.this).content("手机号码非法,请重新输入").positiveText("确定").show();
                }else{
                    AccountRESTFul accountRESTFul=new AccountRESTFul();
                    accountRESTFul.get(new AccountRESTFul.IhttpCb() {
                        @Override
                        public void ok(JSONObject response) {
                            Log.e("sss","授权 成功");
                            codeRESTFul getcodeRequest = new codeRESTFul();
                            getcodeRequest.getCode(RegisterActivity.this, phoneNumber, new codeRESTFul.IgetCodeCb() {
                                @Override
                                public void ok(JSONArray response) {
                                    getCode.setEnabled(false);
                                    timecount.start();
                                    Gson gson = new Gson();
                                    List<String> list = gson.fromJson(response.toString(), new TypeToken<List<String>>() {
                                    }.getType());
                                    CODE = list.get(0);
                                    if (CODE == register_vercode.getText().toString()) {
                                        Log.e("ss", "获取code成功");
                                    }
                                }

                                @Override
                                public void error(String error) {
                                    final MaterialDialog registerSuccess = new MaterialDialog.Builder(RegisterActivity.this)
                                            .content("获取验证码失败！")
                                            .show();
                                    Handler handler = new Handler();
                                    handler.postDelayed(new Runnable() {
                                        public void run() {
                                            registerSuccess.dismiss();//设置弹框消失
                                        }
                                    }, 1000);
                                }
                            });
                        }
                        @Override
                        public void error(String error) {
                            Log.e("eee","授权失败");
                        }
                    });
                }
            }
        });
        //下一步操作
        register_next1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                register_vercode.getText().toString().equals(CODE)&& !CODE.equals("")
                if(register_vercode.getText().toString().equals(CODE)&& !CODE.equals("")){
                    phoneNumber=register_phonenum.getText().toString();
                    Intent intent = new Intent(RegisterActivity.this, RegisterInfoActivity.class);
                    intent.putExtra("number", phoneNumber);
                    startActivity(intent);
                    RegisterActivity.this.finish();
                    Handler handler = new Handler();
                                            handler.postDelayed(new Runnable() {
                                                public void run() {
                                                    MFGT.finish(RegisterActivity.this);
                                                }
                                            },2000);
                } else{
                    new MaterialDialog.Builder(RegisterActivity.this).content("验证码输入错误，请重新输入").positiveText("确定").show();
                }
            }
        });
        //已有账户
        register_account_have.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        });
    }
    //计时内部类
    class timeCount extends CountDownTimer {
        public timeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }
        @Override
        public void onFinish() {// 计时完毕
            getCode.setText("重新获取验证码");
            getCode.setEnabled(true);
            getCode.setTextColor(Color.parseColor("#333333"));
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程
            getCode.setEnabled(false);;//防止重复点击
            getCode.setText(millisUntilFinished / 1000 + "秒重新获取");
            getCode.setTextColor(Color.parseColor("#999999"));
        }
    }
}
